const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const cors = require('cors');

app.use(cors({
    origin:'*'
}));
app.use(express.json());
app.use(express.static(path.join(__dirname, '../public')));

app.post('/save-file', (req, res) => {
    console.log(req)
    const { sourceFile, fileName } = req.body;
    const sourceFilePath = path.join(__dirname, '../public', `${sourceFile}.html`);
    const targetFilePath = path.join(__dirname, '../public', `${fileName}.html`);

    fs.readFile(sourceFilePath, 'utf8', (err, data) => {
        if (err) {
            return res.status(500).send('读取源文件时出错');
        }
        fs.writeFile(targetFilePath, data, 'utf8', (err) => {
            if (err) {
                return res.status(500).send('写入目标文件时出错');
            }
            res.send('文件已成功复制');
        });
    });
});

app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000');
});
